//**************************************************
// ***** HEADER FILE : FAT32.h ******
//**************************************************
#ifndef _FAT32_H_
#define _FAT32_H_

//Structure to access Master Boot Record for getting info about partioions
struct MBRinfo_Structure{
unsigned char     nothing[446];      //ignore, placed here to fill the gap in the structure
unsigned char     partitionData[64]; //partition records (16x4)
unsigned char     signature1;         //0xaa55
unsigned char     signature2;
};

//Structure to access info of the first partioion of the disk
struct partitionInfo_Structure{                      
unsigned char     status;                 //0x80 - active partition
unsigned char     headStart;              //starting head

unsigned char     cylSectStart[2];          //starting cylinder and sector

unsigned char     type;                   //partition type
unsigned char     headEnd;                //ending head of the partition

unsigned char     cylSectEnd[2];            //ending cylinder and sector

unsigned char     firstSector[4];            //total sectors between MBR & the first sector of the partition

unsigned char     sectorsTotal[4];           //size of this partition in sectors
};

//Structure to access boot sector data
struct BS_Structure{
unsigned char jumpBoot[3];      //default: 0x009000EB
unsigned char OEMName[8];
unsigned char bytesPerSector[2];   //deafault: 512
unsigned char sectorPerCluster;
unsigned char reservedSectorCount[2];
unsigned char numberofFATs;
unsigned char rootEntryCount[2];
unsigned char totalSectors_F16[2]; //must be 0 for FAT32
unsigned char mediaType;
unsigned char FATsize_F16[2];      //must be 0 for FAT32
unsigned char sectorsPerTrack[2];
unsigned char numberofHeads[2];
unsigned char hiddenSectors[4];
unsigned char totalSectors_F32[4];
unsigned char FATsize_F32[4];      //count of sectors occupied by one FAT
unsigned char extFlags[2];
unsigned char FSversion[2];        //0x0000 (defines version 0.0)
unsigned char rootCluster[4];      //first cluster of root directory (=2)
unsigned char FSinfo[2];           //sector number of FSinfo structure (=1)
unsigned char BackupBootSector[2];
unsigned char reserved[12];
unsigned char driveNumber;
unsigned char reserved1;
unsigned char bootSignature;
unsigned char volumeID[4];
unsigned char volumeLabel[11];   //"NO NAME "
unsigned char fileSystemType[8]; //"FAT32"
unsigned char bootData[420];
unsigned char bootEndSignature[2];  //0xaa55
};


//Structure to access FSinfo sector data
struct FSInfo_Structure
{
unsigned long leadSignature;      //0x41615252
unsigned char reserved1[480];
unsigned long structureSignature; //0x61417272
unsigned long freeClusterCount;   //initial: 0xffffffff
unsigned long nextFreeCluster;    //initial: 0xffffffff
unsigned char reserved2[12];
unsigned long trailSignature;     //0xaa550000
};

//Structure to access Directory Entry in the FAT
struct dir_Structure{
unsigned char name[11];
unsigned char attrib;         //file attributes
unsigned char NTreserved;     //always 0
unsigned char timeTenth;      //tenths of seconds, set to 0 here
unsigned char createTime[2];     //time file was created
unsigned char createDate[2];     //date file was created
unsigned char lastAccessDate[2];
unsigned char firstClusterHI[2]; //higher word of the first cluster number
unsigned char writeTime[2];      //time of last write
unsigned char writeDate[2];      //date of last write
unsigned char firstClusterLO[2]; //lower word of the first cluster number
unsigned char fileSize[4];       //size of file in bytes
};

struct dir_Long_Structure{
unsigned char sequence_number;  // Last structure has 7th bit checked
unsigned char name_part1[10];
unsigned char attr;  // Should be 0x0F for long structures
unsigned char reserved;
unsigned char DOSchecksum;
unsigned char name_part2[12];
unsigned char first_cluster[2]; // Should be 0x0000
unsigned char name_part3[4];
};

//Attribute definitions for file/directory
#define ATTR_READ_ONLY     0x01
#define ATTR_HIDDEN        0x02
#define ATTR_SYSTEM        0x04
#define ATTR_VOLUME_ID     0x08
#define ATTR_DIRECTORY     0x10
#define ATTR_ARCHIVE       0x20
#define ATTR_LONG_NAME     0x0f


#define DIR_ENTRY_SIZE     0x32
#define EMPTY              0x00
#define DELETED            0xe5
#define GET         0
#define SET         1
#define READ        0
#define VERIFY      1
#define ADD         0
#define REMOVE      1
#define TOTAL_FREE  1
#define NEXT_FREE   2
#define GET_LIST    0
#define GET_FILE    1
#define DELETE          2
#define FAT_EOF         0x0fffffff

#endif
